﻿$(document).ready(function() {
    getRuleList();
    $('#'+ txtCLNumber).on('change',function()
    {
        getRuleList();
    })
        
    $('#divApproval').on('change', '.cbAppList', function() {
        $("#spnAppStat").text('');

        var thisval = $(this).val();
        var divParent = $('#divApproval');
        var divChild = divParent.children();
        var divChildLen = divChild.length;
        var thisIndex = $(this).parent().index();
        var nextIndex = thisIndex + 1;
        if (divChildLen > 1) {
            for (var idx = nextIndex; idx < divChildLen; idx++) {
                divChild.eq(idx).remove();
            }
        }
        if (!(typeof thisval == 'undefined' || thisval == '' || thisval == '0' || thisval == 0)) {
            getAppPersons(nextIndex, thisval);
        }
    });

    $('#' + cbDistributor).change(function() {
        var thisVal = $(this).val();
        var sendData = {
            "DistributorCode": thisVal
        };
        $.ajax({
            type: "POST",
            url: 'BulkUpload.aspx/GetDistributorName',
            data: JSON.stringify(sendData),
            contentType: "application/json;charset=utf-8",
            datatype: "json",
            success: function(data) {
                var result = data.d;
                $('#' + txtDistributor).val(result.Name);
                $('#' + hidDistributor).val(result.Code);
            },
            error: function(jqXHR, statusText, errorText) {
                $("#ModalDialog").closeCZDialog();
                alert(errorText);
            }
        });
        return false;
    });

    $("#divApproval").on('change', '.cbAppList', function() {
        var divParent = $("#divApproval");
        var divChilds = divParent.children('.divAppList').children();
        var divChildLen = divChilds.length;

        var arr = [];
        for (var idx = 0; idx < divChildLen; idx++) {
            arr.push(divChilds.eq(idx).val());
        }
        if ($.inArray("0", arr) < 0) {
            $("#" + txtAppList).val(arr.join(','));
        } else {
            $("#" + txtAppList).val('');
        }

    });
});

function getRuleList() {
    var strData = '{}';
    $.ajax({
        type: "POST",
        url: "BulkUpload.aspx/GetRuleList",
        contentType: "application/json;charset=utf-8",
        data: strData,
        datatype: "json",
        success: function(data) {
            $('#divApproval').html('');
            setCache(data.d);
        }
    });
    return false;
}

function setCache(data) {
    if (data.RuleExist == '1') {
        insertAppList(0, data.AppList);
    } else {
        $('#divApproval').html('');
    }
}

function createAppList(maxLayers) {
    var tmp = '<div class="divAppList">' +
    '<select name="cbAppList[]" class="cbAppList">' +
    '<option value="0">--- Select ---</option>' +
    '</select>' +
    '</div>';

    var maxAppLayers = parseInt(maxLayers);
    var divParent = $('#divApproval');
    var divChildLen = divParent.children().length;

    var cbList = '';
    var idx = divChildLen;
    for (idx; idx < maxAppLayers; idx++) {
        cbList += tmp;
    }
    divParent.html(cbList);
}
function insertAppList(layerIndex, content) {
    var tmp = '<div class="divAppList" style="display:block;">' +
    '<select name="cbAppList[]" class="cbAppList">' +
    '<option value="0">--- Select ---</option>' +
    '</select>' +
    '</div>';

    var divParent = $('#divApproval');
    divParent.append(tmp);
    fillAppList(layerIndex, content);
}
function fillAppList(layerIndex, content) {
    var divParent = $('#divApproval');
    var divChild = divParent.children().eq(layerIndex);
    var divTarget = divChild.children();

    var cLen = 0;
    if (!(typeof content == 'undefined' || content == '')) {
        var opt = '<option value="0">--- Select ---</option>';

        cLen = content.length;
        for (var idx = 0; idx < cLen; idx++) {
            var optKey = '<option value="' + content[idx].appKey + '">';
            var optVal = content[idx].appVal + '</option>';
            opt += optKey + optVal;
        }
        divTarget.html(opt);
        divChild.fadeIn(300);
    }
}
function getAppPersons(layerNum, currentApproval) {
    var nextLayer = parseInt(layerNum + 1);
    var strData = '{"ApprovalLayer":' + nextLayer + ', ';
    strData += '"CurrentApproval":"' + currentApproval + '"}';
    $.ajax({
        type: "POST",
        url: "BulkUpload.aspx/GetApprovalList",
        contentType: "application/json;charset=utf-8",
        data: strData,
        datatype: "json",
        success: function(data) {
            var dLen = data.d.length;
            var divParent = $('#divApproval');
            var divChild = divParent.children();
            var divChildLen = divChild.length;
            if (dLen > 0) {
                var isVisible = divChild.eq(layerNum).is(':visible');
                if (!isVisible) {
                    insertAppList(layerNum, data.d);
                } else {
                    fillAppList(layerNum, data.d);
                }
            } else {
                for (var idx = layerNum; idx < divChildLen; idx++) {
                    divChild.eq(idx).remove();
                }
                var cbval = divChild.eq((layerNum - 1)).children().val();
                if (cbval != '0' || cbval != 0) {
                    //alert('End of Approval Layer');
                    $("#spnAppStat").text(' --- End of Approval Layer ---');
                }
            }
        }
    });
    return false;
}